package com.xinxin.facelinker.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.zxing.WriterException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.QRcode.encoding.EncodingHandler;
import com.xinxin.facelinker.domain.GetPalInfo;
import com.xinxin.facelinker.ui.button.views.ButtonFloat;
import com.xinxin.facelinker.ui.menu.ContextMenuDialogFragment;
import com.xinxin.facelinker.ui.menu.MenuObject;
import com.xinxin.facelinker.ui.menu.MenuParams;
import com.xinxin.facelinker.ui.menu.interfaces.OnMenuItemClickListener;
import com.xinxin.facelinker.ui.menu.interfaces.OnMenuItemLongClickListener;
import com.xinxin.facelinker.utils.NetHelper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class GetPalInfoActivity extends FragmentActivity implements OnMenuItemClickListener,
        OnMenuItemLongClickListener {
    private TextView tvAccountNum;
    private TextView tvPhoneNum;
    private TextView tvNickname;
    private TextView tvGender;
    private TextView tvBirthday;
    private TextView tvMood;
    private ImageView ivHonour;
    private ImageView ivPhoto;
    private String my_account_num;
    private String other_account_num;
    HttpUtils httpUtils = new HttpUtils();
    private View btnBack;
    private ImageLoader loader;
    private String account_num="123456789";
    private ImageView imgQRcode;
    private ButtonFloat btnMenu;
    private FragmentManager fragmentManager=getSupportFragmentManager();
    private DialogFragment mMenuDialogFragment;
    private ImageView ivBackground;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_pal_info);

        tvPhoneNum= (TextView) findViewById(R.id.tvPhoneNum);
        tvAccountNum = (TextView) findViewById(R.id.tvAccountNum);
        tvNickname = (TextView) findViewById(R.id.tvNickname);
        tvGender = (TextView) findViewById(R.id.tvGender);
        tvBirthday = (TextView) findViewById(R.id.tvBirthday);
        tvMood = (TextView) findViewById(R.id.tvMood);
        ivHonour= (ImageView) findViewById(R.id.ivHonour);
        ivPhoto = (ImageView) findViewById(R.id.ivPhoto);
        btnBack=findViewById(R.id.btnBack);
        btnMenu= (ButtonFloat) findViewById(R.id.btnMenu);
        ivBackground= (ImageView) findViewById(R.id.ivBackground);

        initMenuFragment();

        loader=ImageLoader.getInstance();
        ivPhoto.setImageResource(R.drawable.photo);

        my_account_num = Config.getCachedAccountNum(GetPalInfoActivity.this);
        other_account_num = getIntent().getStringExtra(Config.KEY_OTHER_ACCOUNT_NUM);

        imgQRcode = (ImageView) findViewById(R.id.imgQRcode);
        //account_num之后给出
//        account_num= Config.getCachedAccountNum(CreateQRcodeActivity.this);

        try {
            Bitmap qrcode = EncodingHandler.createQRCode(account_num, 800);
            imgQRcode.setImageBitmap(qrcode);
        } catch (WriterException e) {
            e.printStackTrace();
        }

        btnMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMenuDialogFragment.onAttach(GetPalInfoActivity.this);
                fragmentManager.beginTransaction().add(mMenuDialogFragment,"").commit();
            }
        });

        final ProgressDialog pd = ProgressDialog.show(GetPalInfoActivity.this, getResources().getString(R.string.get_contacts_connecting), getResources().getString(R.string.get_pal_info_connecting_to_server));
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_GET_PAL_INFO);
        params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
        params.addBodyParameter(Config.KEY_OTHER_ACCOUNT_NUM, other_account_num);

        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                pd.dismiss();

                GetPalInfo getPalInfo =
                        (GetPalInfo) NetHelper.parseJsonData(responseInfo.result, GetPalInfo.class);

                tvPhoneNum.setText(getPalInfo.getPhone_num());
                tvAccountNum.setText(getPalInfo.getOther_account_num());
                tvNickname.setText(getPalInfo.getNickname());
                tvGender.setText(getPalInfo.getGender());
                tvBirthday.setText(getPalInfo.getBirthday());
                tvMood.setText(getPalInfo.getMood());
                loader.displayImage(getPalInfo.getHonour_url(),ivHonour);
                loader.displayImage(getPalInfo.getPhoto_url(), ivPhoto);
                loader.displayImage(getPalInfo.getPicture_url(),ivBackground);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                pd.dismiss();
                Toast.makeText(GetPalInfoActivity.this, R.string.get_pal_info_fail, Toast.LENGTH_SHORT).show();
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode== KeyEvent.KEYCODE_BACK){
            finish();
        }

        return true;
    }

    private void initMenuFragment() {
        MenuParams menuParams = new MenuParams();
        menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
        menuParams.setMenuObjects(getMenuObjects());
        menuParams.setClosableOutside(false);
        mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    }

    private List<MenuObject> getMenuObjects() {

        List<MenuObject> menuObjects = new ArrayList<>();

        MenuObject close = new MenuObject();
        close.setResource(R.drawable.btn_close);

        MenuObject send = new MenuObject("发消息");
        send.setResource(R.drawable.btn_send_message);

        MenuObject addPal = new MenuObject("加为好友");
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_add_pal);
        addPal.setBitmap(b);

        MenuObject addBlackList = new MenuObject("加入黑名单");
        BitmapDrawable bd = new BitmapDrawable(getResources(),
                BitmapFactory.decodeResource(getResources(), R.drawable.btn_blacklist));
        addBlackList.setDrawable(bd);

        menuObjects.add(close);
        menuObjects.add(send);
        menuObjects.add(addPal);
        menuObjects.add(addBlackList);
        return menuObjects;
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_image:
                if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                    mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
            mMenuDialogFragment.dismiss();
        } else{
            finish();
        }
    }

    @Override
    public void onMenuItemClick(View clickedView, int position) {
        if(position==1){

            //发送消息待实现

        }else if (position==2){
            RequestParams params = new RequestParams();
            params.addBodyParameter(Config.ACTION, Config.ACTION_ACCEPT_ADD_NEW_PAL);
            params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
            params.addBodyParameter(Config.KEY_OTHER_ACCOUNT_NUM, other_account_num);
            httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Intent intent = new Intent(GetPalInfoActivity.this, SendNewPalMessageActivity.class);
                    startActivity(intent);
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Toast.makeText(GetPalInfoActivity.this, R.string.add_new_pal_fail, Toast.LENGTH_SHORT).show();
                }
            });
        }else if (position==3){
            RequestParams params = new RequestParams();
            params.addBodyParameter(Config.ACTION, Config.ACTION_ADD_BLACKLIST);
            params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
            params.addBodyParameter(Config.KEY_OTHER_ACCOUNT_NUM, other_account_num);
            httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    Toast.makeText(GetPalInfoActivity.this, R.string.add_blacklist_success, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Toast.makeText(GetPalInfoActivity.this, R.string.add_blacklist_fail, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onMenuItemLongClick(View clickedView, int position) {
    }
}
